//
//  TSZEmoticonsAttachment.swift
//  EmotionsTest
//
//  Created by Tsz on 15/10/19.
//  Copyright © 2015年 Tsz. All rights reserved.
//

import UIKit

class TSZEmoticonsAttachment: NSTextAttachment {

 /// 记录表情符号
    var chs: String?
    
    //通过表情符号建立一个使用TSZEmoticonsAttachment  建立的属性文本
    
    class func imageText(emoticon: TSZEmoticons , font: UIFont) ->NSAttributedString {
        
        //创建图片属性字符串
        let attachment  = TSZEmoticonsAttachment()
        
        //记录表情文字
        attachment.chs = emoticon.chs
        
        attachment.image = UIImage(contentsOfFile: emoticon.imagePath)
        //设置边界
        let h = font.lineHeight
        
        //提示：在 scrollView 中，bounds 的原点 就是 contentOffset
        
        attachment.bounds = CGRect(x: 0, y: -4, width: h, height: h)
        
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        //添加文本字体的属性
        
        imageText.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, 1))
        
        return imageText
    }
}
